﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace Accountant.Utils
{
  public class HideWhenDataContextIsNullBehavior : Behavior<Control>
  {
    protected override void OnAttached()
    {
      base.OnAttached();

      AssociatedObject.DataContextChanged += (sender, args) => Update(args.NewValue);
      Update(AssociatedObject.DataContext);
    }

    private void Update(object dataContext)
    {
      AssociatedObject.Visibility = 
        dataContext == null ? Visibility.Collapsed : Visibility.Visible;
    }
  }
}